The present invention is related to computer software and more specifically to computer software for intraprocess communication.
Two computer processes can communicate using a shared file. If communication is in one direction, one process known as a xe2x80x9cwriterxe2x80x9d writes a file, and the other process, known as the xe2x80x9creaderxe2x80x9d reads the file. Conventional processes use an operating system to write and read the file. Each operating system may use a slightly different arrangement to write or read files.
To produce the shared file using some conventional operating systems, the writer sends an xe2x80x9copen for writexe2x80x9d command to the operating system, including a filename or other identifier with which to name the file. The operating system may create the file and open it. Additionally, the operating system may lock the file in response to such a command in order to prohibit use of the file by other processes while the file is being written. The writer may receive from some conventional operating systems a pointer or other identifier of the file in response to the open for write command.
Once the file is opened, the writer uses a buffer in memory to communicate to the operating system the information to be written to the file. The writer stores the information in the buffer, and sends a xe2x80x9cwritexe2x80x9d command to the operating system. The identifier of the file received from the operating system and a pointer to the buffer may be included as part of the write command in order to identify the file and the buffer.
The buffer may be allocated in any of several ways. The writer may allocate as a buffer a portion of the memory allocated to the writer by the operating system and pass a pointer to the buffer with the write command. The operating system may allocate the buffer and pass a pointer to the buffer to the writer in response to the open for write command. The buffer may be allocated using other methods as well.
When the operating system receives the write command, the operating system copies the contents of the buffer to the file. The writer may repeat the xe2x80x9cwritexe2x80x9d command after placing additional information into the buffer again and again, until the writer has finished writing the file. The writer then sends to the operating system a xe2x80x9cclosexe2x80x9d command. The operating system then places an xe2x80x9cEOFxe2x80x9d, or end-of-file marker, at the end of the file to alert any process that may read the file when the end of the file has been reached.
Once the file has been written, a similar process may be used to read the file. An xe2x80x9copen for readxe2x80x9d command containing the filename of the file to be read is sent to the operating system by the reader. The operating system allocates in memory a buffer which it will use to copy some or all of the file. When the reader sends the operating system a xe2x80x9creadxe2x80x9d command, the operating system copies some or all of the file into the buffer and returns to the reader a pointer to the buffer. The reader may use the pointer to retrieve the information copied into the buffer. The reader can repeat the read command again and again until it reads the end-of-file marker, at which point the reader closes the file by sending to the operating system a xe2x80x9cclosexe2x80x9d command.
The use of a shared file requires the reader to wait until the entire file is written by the writer before the reader is able to read the contents of the file. This time can delay the start of processing of the information in the file by the reader. Additionally, if the amount of data in the file is large, significant storage resources may be required to store the file for such intraprocess communication. If these resources are not available, other communication methods must be employed.
One alternative communication method is to allow the two processes to communicate with one another directly. However, such communication may be more complex to implement than the use of the shared file approach described above. Additionally, if the two processes have already been designed for use with a shared file, rewriting the processes to facilitate direct communication may be highly complex.
Therefore, the system and method are needed to allow rapid communications to exist between processes designed for use with a shared file, without requiring the reader to wait until the writer completes writing a shared file, consuming significant system resources, or requiring significant alteration to the processes.
A method and apparatus intercepts from receipt by the operating system commands used to write and read shared files. When the writer fills a buffer and issues a write command, operation of the writer is suspended. Instead of writing the buffer to a file to be shared, the method and apparatus passes the data, for example by passing a pointer to the buffer, to the reader for immediate reading. When the reader completes reading the buffer, the writer is allowed to resume operation. The method and apparatus repeats the process until the writer issues a close command. At that point, an EOF indication is written into the buffer or otherwise passed to the reader. If desired, the method and apparatus can also suspend the writer when it sends the open for write command, until such time as the reader sends an open for read command, allowing the method and apparatus to more closely synchronize the operation of the two processes. Other embodiments of the method and apparatus utilize two or more buffers. The information is copied from the buffer used by the writer into a second buffer for use by the reader. This allows the suspension of the writer to end more quickly while still preventing subsequent operation of the writer from overwriting the information before the reader has had a chance to read it. A flag may be employed to indicate when the reader has started reading the second buffer. The flag may be used to additionally reduce the time the writer is suspended while ensuring that the buffer used by the writer is not copied into the second buffer before the reader has had a chance to read it.
Because the reader receives information from the writer as the writer writes it, the reader can begin processing the information received without waiting for the writer to send all of the information. Because less than all of the information produced by the writer may be stored at any given time, storage requirements may be significantly reduced when compared with the storage of the entire shared file. Because all of the commands sent between the writer and the method and apparatus are conventional commands that would have been used in shared file communications between the writer and the operating system, the writer requires no modification to operate with the method and apparatus of the present invention. Because all or nearly all of the commands sent between the reader and the method and apparatus are conventional commands that would have been used in shared file communications between the reader and the operating system, the reader requires no or little modification to operate with the method and apparatus of the present invention.